Python bottle

  1. Use route to indicate path, 80 is the default port for HTTP.

    1
    2
    3
    4
    5
    6
    7
    import bottle

    @bottle.route('/hello')
    def hello():
    return "Hello World!"

    bottle.run(host='localhost', port=80, debug=True)
  2. Dynamic path V.S. static path. We can use filter such as <id:int> to convert input variables to certain type.

    1
    2
    3
    4
    5
    6
    7
    import bottle

    @bottle.route('/hello/&lt;name>/&lt;address>')
    def hello(name,address):
    return "Hello "+name

    bottle.run(host='localhost', port=80, debug=True)
  3. Use local resources. Pay special attention to the path. Local resource names should start with ‘/‘.

    1
    2
    3
    @bottle.route('/<filename:path>')
    def server_static(filename):
    return bottle.static_file(filename, root='./resource/')
  4. The default HTTP request method for route() is get(), we can use other methods post(), put(), delete(), patch(). The POST method is commonly used for HTML form submission. When entering URL address, GET method is used.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    import bottle

    def check_login(username, password):
    if username=='niuli' and password=='niuli':
    return True
    else:
    return False

    @bottle.get('/login') # or @route('/login')
    def login():
    return '''
    &lt;form action="/login" method="post">
    Username: &lt;input name="username" type="text" />
    Password: &lt;input name="password" type="password" />
    &lt;input value="Login" type="submit" />
    &lt;/form>
    '''

    @bottle.post('/login') # or @route('/login', method='POST')
    def do_login():
    username = bottle.request.forms.get('username')
    password = bottle.request.forms.get('password')
    if check_login(username, password):
    return "&lt;p>Your login information was correct.&lt;/p>"
    else:
    return "&lt;p>Login failed.&lt;/p>"

    bottle.run(host='localhost', port=80, debug=True)
  5. Return local files localhost/filename. Note path:path is important, otherwise the filename containing ‘/‘ may not be correctly recognized.

    1
    2
    3
    4
    5
    from bottle import static_file

    @route(’/<filepath:path>’)
    def server_static(filepath):
    return static_file(filepath, root=’./resource’)
  6. Use redirect to jump to another page: bottle.redirect('/login')

  7. Use the HTML template. In the template file, the lines starting with % are python codes and others are HTML codes. We can include other templates in the current template by using % include('header.tpl', title='Page Title'). Cookies, HTTP header, HTML <form> fields and other request data is available through the global request object.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    @bottle.route('/<name>')
    @bottle.view('my_template.html')
    def root(name="default"):
    return bottle.template('my_template', name=name)

    @bottle.post('/login') # or @route('/login', method='POST')
    def do_login():
    username = bottle.request.forms.get('username')
    password = bottle.request.forms.get('password')
    bottle.redirect('/'+username)
  8. Some tricks for helping development

    • bottle.debug(True) The default error page shows a traceback. Templates are not cached. Plugins are applied immediately.

    • run(reloader=True) Every time you edit a module file, the reloader restarts the server process and loads the newest version of your code.